<?php

namespace Main\Api\Admin;

use FC\Json;

// 控制器基类
use FC\Controller\BaseController;

class login extends BaseController
{
    
    // 登录验证
    public function index()
    {
        $email = \FC\post('email');
        $pass = \FC\post('pass');
        $vercode = \FC\post('vercode');
		$adminsModel = $this->admins_model;
        $server_vercode = $this->SESSION->get('vercode');
        if (!$adminsModel->checkInput('email', $email)) {
            Json::result([], 1, '邮箱格式不正确');
        }
        if (empty($vercode) || $vercode != $server_vercode) {
            Json::result([], 1, '验证码不正确');
        }
        $userInfo = $adminsModel->getOnly(['email'=>$email]);
        if (!$userInfo) {
            Json::result([], 1, '账号或密码错误');
        }
        $server_pass = $userInfo['password'];
        if (empty($pass) || ($adminsModel->Encrypt($pass) != $server_pass)) {
            Json::result([], 1, '密码错误');
        }
        if ($userInfo['status'] == 0) {
            Json::result([], 1, '该账号已禁止登陆');
        }
        // sid , 又名 token
        $sid = md5($server_pass . $userInfo['email'] . TIME);
        $datas = [
            'sid'     => $sid,
            'udat'    => TIME
        ];
        if ($adminsModel->checkUpdate($datas, ['uid'=>$userInfo['uid']])) {
            $user = [];
            $user['token'] = $sid;
            $user['user']['username'] = $userInfo['username'];
            Json::result($user, 0, 'ok');
        } else {
            Json::result([], 1, '登录失败');
        }
    }
}
